﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;

namespace Snom
{
    class Browser
    {
        private CookieContainer _cookieContainer;

        public Browser() : this(null)
        {
        }

        public Browser(Uri proxyUri)
        {
            ProxyUri = proxyUri;
            ServicePointManager.Expect100Continue = false;
            ServicePointManager.SetTcpKeepAlive(true, 10000, 1000);
            _cookieContainer = new CookieContainer();
        }

        public Uri ProxyUri
        {
            get;
            set;
        }

        private void AddHeaders(HttpWebRequest request)
        {
        //GET http://172.16.5.58/command.htm?key=0 HTTP/1.1
        //Accept: text/html, application/xhtml+xml, */*
        //Accept-Language: he-IL
        //User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
        //Accept-Encoding: gzip, deflate
        //Connection: Keep-Alive
        //Host: 172.16.5.58
        //Pragma: no-cache

            request.KeepAlive = true;
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.Headers.Add("Accept-Language", "en-us");
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E)";

            request.Headers.Add(HttpRequestHeader.Pragma, "no-cache");
            
            // Missing headers:
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        }

        internal Stream GetHttpPostStream(string url, string referer, string contentType, string content)
        {
            HttpWebRequest request = CreateHttpRequest(url, referer, "POST", false, contentType, content);
            HttpWebResponse response = ExecuteHttpRequest(request);

            Stream stream = response.GetResponseStream();

            return stream;
        }

        internal HttpWebResponse ExecuteHttpRequest(HttpWebRequest request)
        {
            if (ProxyUri != null)
            {
                request.Proxy = new WebProxy(ProxyUri);
            }

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            return response;
        }

        internal HttpWebResponse ExecuteHttpGetRequest(string url, string referer, bool allowAutoRedirect)
        {
            HttpWebRequest request = CreateHttpGetRequest(url, referer, allowAutoRedirect);
            return ExecuteHttpRequest(request);
        }

        public HttpWebResponse ExecuteHttpGetRequest(string url)
        {
            return ExecuteHttpGetRequest(url, null, true);
        }

        public void ExecutePhoneRequestNoResponse(string url)
        {
            using (HttpWebResponse response = ExecuteHttpGetRequest(url))
            {
            }
        }

        public string GetContent(string url)
        {
            using (HttpWebResponse response = ExecuteHttpGetRequest(url))
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                return reader.ReadToEnd();
            }
        }

        internal HttpWebRequest CreateHttpGetRequest(string url, string referer, bool allowAutoRedirect)
        {
            return CreateHttpRequest(url, referer, null, allowAutoRedirect, null, null);
        }

        public HttpWebRequest CreateHttpRequest(string url, string referer, string method, bool allowAutoRedirect, string contentType, string content)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.CookieContainer = _cookieContainer;

            if (referer != null)
            {
                request.Referer = referer;
            }

            if (method != null)
            {
                request.Method = method;
            }

            request.AllowAutoRedirect = allowAutoRedirect;

            // Write all the headers before writing the stream
            AddHeaders(request);

            if (content != null)
            {
                request.ContentType = contentType;
                request.ContentLength = content.Length;

                StreamWriter writer = new StreamWriter(request.GetRequestStream());

                writer.Write(content);
                writer.Close();
            }

            return request;
        }

        public HttpWebRequest CreateHttpGetRequest(string url)
        {
            return CreateHttpGetRequest(url, null, true);
        }

    }
}
